package com.mingyuan.library.domain.lending.patron.event;

import com.mingyuan.library.domain.common.event.DomainEvent;
import io.vavr.collection.List;
import io.vavr.control.Option;
import lombok.NonNull;
import lombok.Value;

import java.time.Instant;
import java.util.UUID;

/**
 * 已预定图书事件
 *
 * @author yangzk
 * @date 2023/12/28 19:31
 **/
@Value
public class BookPlacedOnHoldEvents implements PatronEvent {
    /**
     * 事件ID
     */
    @NonNull
    UUID eventId = UUID.randomUUID();
    /**
     * 读者ID
     */
    @NonNull UUID patronId;
    /**
     * 已预定图书
     */
    @NonNull BookPlacedOnHold bookPlacedOnHold;
    /**
     * 达到预定最大数量
     */
    @NonNull Option<MaximumNumberOhHoldsReached> maximumNumberOhHoldsReached;

    @Override
    public Instant getWhen() {
        return bookPlacedOnHold.getWhen();
    }

    public static BookPlacedOnHoldEvents events(BookPlacedOnHold bookPlacedOnHold) {
        return new BookPlacedOnHoldEvents(bookPlacedOnHold.getPatronId(), bookPlacedOnHold, Option.none());
    }

    public static BookPlacedOnHoldEvents events(BookPlacedOnHold bookPlacedOnHold, MaximumNumberOhHoldsReached maximumNumberOhHoldsReached) {
        return new BookPlacedOnHoldEvents(bookPlacedOnHold.getPatronId(), bookPlacedOnHold, Option.of(maximumNumberOhHoldsReached));
    }

    public List<DomainEvent> normalize() {
        return List.<DomainEvent>of(bookPlacedOnHold).appendAll(maximumNumberOhHoldsReached.toList());
    }
}